package cn.the.angry.sprite;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import cn.the.angry.util.GraphicUtils;

public class Sprite extends BaseSprite {
	private int offsetX=0, offsetY=0;
	
	public Sprite(BufferedImage[] _frames) {
		super(_frames);
	}

	@Override
	public void draw(Graphics2D g) {
		super.draw(g);
		BufferedImage img = getCurFrame();
		int max = (int)Math.sqrt(fWidth*fWidth+fHeight*fHeight);
		if(angle != 0){
			g.drawImage(GraphicUtils.rotateImage(getCurFrame()
					, fWidth, fHeight, angle), (int) (x-max/2+fWidth/2-offsetX*Math.abs(Math.cos(angle))) 
					, (int) (y-max/2+fHeight/2-offsetY*Math.abs(Math.cos(angle))), null);
			return;
		}
		g.drawImage(img, (int)x-offsetX, (int)y-offsetY, null);
		
		//update
		update();
	}

	public void setOffset(int offsetX, int offsetY)
	{
		this.offsetX = offsetX;
		this.offsetY = offsetY;
	}
	
	public static void draw(Graphics2D g, final BufferedImage image, final int x, final int y) {
		int w = image.getWidth();//fWidth
		int h = image.getHeight();//fHeight
		int max = (int)Math.sqrt(w*w+h*h);
		g.drawImage(image, x, y, null);
	}
}
